const path =require('path')
const { findSearchGood,createGoodsItem,updateGoods,removeItem,changeStore,findGoods,findOne,findMyAll,findSomeAllData,findListInfo,findMySaleOut }=require('../service/goods.service')
class GoodsController{
    async uploadImage(ctx,next){
        //console.log(ctx.request.files.file)
        const {file}=ctx.request.files
        const fileType=['image/jpeg','image/png','image/webp']
        if(file){
            if(!fileType.includes(file.type)){
                ctx.body={
                    code:10090,
                    message:"请上传图片",
                    result:"出错了，很无语"

                }
            }else {
                ctx.body={
                    code:0,
                    message:"图片上传成功",
                    result:{
                        goods_img:path.basename(file.path)

                    }
                }
            }

        }else{
            ctx.body={
                code:10090,
                message:"图片上传失败",
                result:"出错了，很无语"

            }
        }

    }
    async createGoods(ctx){
       //直接调用server操作数据库
        const user_id=ctx.state.user.id
        ctx.request.body.send_id=user_id
        try {

          const res=  await createGoodsItem(ctx.request.body)
           return  ctx.body={
              code:0,
              result:res
            }}
        catch (err){

        }

    }
    async update(ctx){

      try {
          const res= await updateGoods(ctx.params.id,ctx.request.body)
          if(res){
              ctx.body={
                  code:0,
                  result:"修改成功"
              }
          }else {
              return ctx.body={
                  code:11111,
                  result:"修改商品不存在"
              }
          }
      }catch (err){
          ctx.body={
              code:0,
              result:"修改成功"
          }

      }

    }
    async remove(ctx){
      const res=  await removeItem(ctx.params.id)
        console.log(res)
        if(res){
            ctx.body={
                result:"删除成功"
            }
        }else {
            ctx.body={
                result:"删除失败"
            }
        }
    }
    async offStore(ctx){
        const res=await changeStore(ctx.params.id,ctx.request.body.state)
        if(res){
            ctx.body={
                result:"下架成功"
            }
        }else {
            ctx.body={
                result:"下架失败"
            }
        }
    }
    async findAll(ctx){
        //解析出俩参数
        const {pageNum=1,pageSize=10}=ctx.request.query
        const res=await findGoods(pageNum,pageSize)
        ctx.body={
            message:"获取商品列表成功",
            result:res
        }
    }
    async findOne(ctx){
        const {id}=ctx.request.query
        console.log(id)
        const res=await findOne(id)
        ctx.body={
            message:"获取商品信息成功",
            result:res
        }
    }
    async findMySend(ctx){
        const user_id=ctx.state.user.id
        const res=await findMyAll(user_id)
        ctx.body={
            code:0,
            message:"获取成功",
            result:res
        }
    }
    async findSomeAll(ctx){
        const {goods_classify}=ctx.request.query
        const {pageNum=1,pageSize=10}=ctx.request.query
        if(goods_classify===undefined){
            return ctx.body={
                code:444,
                message:"没有参数"
            }
        }
        const res= await findSomeAllData(goods_classify,pageSize,pageNum)
        ctx.body={
            code:0,
            message:"获取成功",
            result:res
        }
    }
    async findListGoods(ctx){
        let ids=JSON.parse(ctx.request.query.ids)
        console.log(typeof ids)
        const  res=await findListInfo(ids)
        ctx.body={
             message:"ok",
            result:res
        }
    }
    async mySaleOut(ctx){
        const user_id=ctx.state.user.id
        const res=await findMySaleOut(user_id)
        ctx.body={
            result:res,
            message:"获取卖出成功"
        }
    }
    async searchGoodsList(ctx){
        const keyWord=ctx.request.query.keyWord
        const res=await findSearchGood(keyWord)
        ctx.body={
            code:0,
            message:"获得搜索成功",
            result:res
        }

    }
}


module.exports=new GoodsController()